# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Alexey Pavlov <alexpux@gmail.com>

_realname=gdb
_gcc_ver=$(gcc -v 2>&1 | grep "^gcc version" | head -n1 | cut -c 13- | sed -r "s/\\s.*$//")
pkgbase=mingw-w64-${_realname}-git
pkgver=r94080.07784cb5da
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgrel=1
pkgdesc="GNU Debugger (mingw-w64)"
arch=('any')
url="https://www.gnu.org/software/gdb/"
license=('GPL')
depends=("${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-python2"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-winpthreads")
#checkdepends=('dejagnu' 'bc')
makedepends=("${MINGW_PACKAGE_PREFIX}-iconv"
             "${MINGW_PACKAGE_PREFIX}-ncurses"
             "${MINGW_PACKAGE_PREFIX}-xz"
             "git")
options=('staticlibs' '!distcc' '!ccache' 'debug' '!strip')
source=("${_realname}"::"git+https://sourceware.org/git/binutils-gdb.git#branch=master"
        gdbinit
        0001-MinGW-w64-Two-fixes-for-unusual-files.patch
        0002-MinGW-w64-Fix-libibery-configure.patch
        0003-MinGW-w64-Use-gnu-printf.patch
        0004-GDB-performance.patch
        0005-Mitigate-gdb-bug-21057.patch
        0006-Disable-checks-using-setenv-or-unsetenv-as-they-do-n.patch
        0007-Make-GDB-compile-on-MinGW-targets-again.patch
        0008-Avert-the-crash-when-using-info-register-on-Windows-.patch)
sha256sums=('SKIP'
            'dea2bbad4967280910559c6a11b865aeec19cab34647fb5894cb498b24b14462'
            '9f02f6740fc5a8e0900e2ec0196bb5851f5a5f06a4da162434646dff7fef4e54'
            'c92183a52f53d82cb5ffddf2a18f5bddd350ccc0db01eff1e5918d98467ee8e3'
            'c8aab67618e05c31e3b60b59319d8fa3eeb7ca9dbf8de937a07c99533557dd5f'
            '238e6099343854ef664bbf10a4109873d6a52a0d2f0515899db4821f1734a23e'
            '9d2aa8b97952e92973b4c306aafbf338c1cd6c1bb1f75f81bdf85751464a9750'
            '4b21429e3583866b25ca4feb6ae57daab81d8735d1eb52ab61338184f58bc2fb'
            '11f37b0bcd840ef74530092bedda33d41d0820d46af1f252f36bdb12d9b4e712'
            '41e464ea41aa3d2c603a4c411d029036d47745e43f569db3b5e6e457180eab10')

pkgver() {
  cd "$srcdir/$_realname"
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${srcdir}/${_realname}
  git am "${srcdir}"/0001-MinGW-w64-Two-fixes-for-unusual-files.patch
  git am "${srcdir}"/0002-MinGW-w64-Fix-libibery-configure.patch
  git am "${srcdir}"/0003-MinGW-w64-Use-gnu-printf.patch
  git am "${srcdir}"/0004-GDB-performance.patch
  git am "${srcdir}"/0005-Mitigate-gdb-bug-21057.patch
  git am "${srcdir}"/0006-Disable-checks-using-setenv-or-unsetenv-as-they-do-n.patch
  git am "${srcdir}"/0007-Make-GDB-compile-on-MinGW-targets-again.patch
  git am "${srcdir}"/0008-Avert-the-crash-when-using-info-register-on-Windows-.patch
}

build() {
  declare -a _extra_conf
  if [ "${CARCH}" = "x86_64" ]; then
    _extra_conf+=(--enable-64-bit-bfd)
  else
    LDFLAGS+=" -Wl,--large-address-aware"
  fi

  if check_option "debug" "y"; then
    CFLAGS+=" -O0"
  fi

  CFLAGS+=" -pthread"
  LDFLAGS+=" -pthread"

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}

  mkdir ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}
  ../${_realname}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-targets="i686-w64-mingw32,x86_64-w64-mingw32" \
    "${_extra_conf[@]}" \
    --disable-werror \
    --disable-win32-registry \
    --disable-rpath \
    --with-system-gdbinit=${MINGW_PREFIX}/etc/gdbinit \
    --with-python=${MINGW_PREFIX}/bin/python-config-u.sh \
    --with-expat \
    --with-libiconv-prefix=${MINGW_PREFIX} \
    --with-zlib \
    --with-lzma \
    --with-system-readline \
    --enable-tui

  make all-gdb
}

# check() {
#   cd "${srcdir}"/build-${MINGW_CHOST}
#
#   # unset LDFLAGS as testsuite makes assumptions about which ones are active
#   # do not abort on errors - manually check log files
#   make LDFLAGS="" -k check || true
# }

package() {
  cd "${srcdir}"/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install-gdb

  # Remove unwanted files
  rm -rf ${pkgdir}${MINGW_PREFIX}/share/{man,info}

  # install "custom" system gdbinit
  install -D -m644 ${srcdir}/gdbinit ${pkgdir}${MINGW_PREFIX}/etc/gdbinit
  sed -i 's|%GCC_NAME%|gcc-'${_gcc_ver}'|g' ${pkgdir}${MINGW_PREFIX}/etc/gdbinit
}
